package com.call.widget;

import com.call.R;
import com.call.activity.MainActivity;
import com.call.activity.SMSActivity;
import com.call.service.MissCallAndPhoneNumberService;

import android.app.ActivityManager;
import android.app.PendingIntent;
import android.app.ActivityManager.RunningServiceInfo;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class WidgetProvider extends AppWidgetProvider{

	public static final String UPDATE_ACTION = "UPDATE_ACTION";
	//public static final String START_ACTION = "START_ACTION";
	public static boolean startService = false;
	private final String TAG = this.getClass().getSimpleName();
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		
		Log.v(TAG,"->> onUpdate");
		
		ComponentName me=new ComponentName(context, WidgetProvider.class);
		appWidgetManager.updateAppWidget(me, buildUpdate(context, appWidgetIds));
	}
	
	 private RemoteViews buildUpdate(Context ctxt, int[] appWidgetIds) {
		    RemoteViews updateViews=new RemoteViews(ctxt.getPackageName(),
		                                            R.layout.widget_layout);
		    
		    // Su ly nut ON/OFF
		    Intent i=new Intent(ctxt, WidgetProvider.class);
		    i.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
		    i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
		    PendingIntent pi_startService=PendingIntent.getBroadcast(ctxt, 0 , i, PendingIntent.FLAG_UPDATE_CURRENT);
		    
		    
		    //boolean isStartedService = ;
		    Intent service = new Intent(ctxt, MissCallAndPhoneNumberService.class);
		    
		    if (MissCallAndPhoneNumberService.isInstanceCreated())
		    {
		    	
		    	// Tat service dang chay
		    	
		    	ctxt.stopService(service);
		    	
		    	//MainActivity.this.isChild()
		    	
		    	Toast.makeText(ctxt,  "Tat 111 Services" , Toast.LENGTH_SHORT).show();
		    	startService = false;
		    	updateViews.setImageViewResource(R.id.bnt_startServices,R.drawable.bnt_off);
		    	
		    } else
		    {
		    	ctxt.startService(service);
		    	
		    	Toast.makeText(ctxt,  "BAT Services" , Toast.LENGTH_SHORT).show();
		    	//startService = true;
		    	updateViews.setImageViewResource(R.id.bnt_startServices,R.drawable.bnt_on);
		    }
		    
		    
		  /*  if (startService)
		    {
		    	// Tat service dang chay
		    	
		    	ctxt.stopService(service);
		    	
		    	//MainActivity.this.isChild()
		    	
		    	Toast.makeText(ctxt,  "Tat 111 Services" , Toast.LENGTH_SHORT).show();
		    	startService = false;
		    	updateViews.setImageViewResource(R.id.bnt_startServices,R.drawable.bnt_off);
		    } else {
		    	
		    	//Intent service = new Intent(ctxt, MissCallAndPhoneNumberService.class);
		    	ctxt.startService(service);
		    	
		    	Toast.makeText(ctxt,  "BAT Services" , Toast.LENGTH_SHORT).show();
		    	startService = true;
		    	updateViews.setImageViewResource(R.id.bnt_startServices,R.drawable.bnt_on);
		    }*/
		    
		    updateViews.setOnClickPendingIntent(R.id.bnt_startServices, pi_startService);
		    
		    
		    // sy ly nut setting
		    Intent i_setting  = new Intent(ctxt, SMSActivity.class);
		    PendingIntent pi_setting = PendingIntent.getBroadcast(ctxt,0, i,0);
		    updateViews.setOnClickPendingIntent(R.id.bntSetting, pi_setting);
		    
		    
		    return(updateViews);
		  }
	 
	 private RemoteViews buildUpdate2(Context ctxt, int[] appWidgetIds) {
		    RemoteViews updateViews=new RemoteViews(ctxt.getPackageName(),
		                                            R.layout.widget_layout);
		    
		  
		    Intent service = new Intent(ctxt, MissCallAndPhoneNumberService.class);
		    
		    if (!MissCallAndPhoneNumberService.isInstanceCreated())
		    {
		    	//startService = false;
		    	Toast.makeText(ctxt,  "Dang Tat Services" , Toast.LENGTH_SHORT).show();
		    	updateViews.setImageViewResource(R.id.bnt_startServices,R.drawable.bnt_off);
		    } else {
		    	//startService = true;
		    	Toast.makeText(ctxt,  "Dang BAT Services" , Toast.LENGTH_SHORT).show();
		    	updateViews.setImageViewResource(R.id.bnt_startServices,R.drawable.bnt_on);
		    }
		    
		    
		    return(updateViews);
		  }
	 
	 
	 @Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		 Log.v(TAG, "onReceive");
		 String action = intent.getAction(); 
         if (action != null && action.equals(UPDATE_ACTION)) { 
               //  final AppWidgetManager manager = AppWidgetManager.getInstance (context); 
               //  onUpdate(context, manager, 
               //                  manager.getAppWidgetIds(new ComponentName( 
               //                                  context, WidgetProvider.class) 
               //                  ) 
               //  ); 
        	
        	 final AppWidgetManager manager = AppWidgetManager.getInstance (context); 
        	 int[] appWidgetIds =  manager.getAppWidgetIds(new ComponentName( context, WidgetProvider.class)) ;
        	 ComponentName me=new ComponentName(context, WidgetProvider.class);
        	 manager.updateAppWidget(me, buildUpdate2(context, appWidgetIds));
        	 
        	 
         } 
         else { 
                 super.onReceive(context, intent); 
         } 
	}
}
